<?php
// +----------------------------------------------------------------------
// | MyCentOS
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.mycentos.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 琥珀 <317559272@qqq.com><http://www.mycentos.com>
// +----------------------------------------------------------------------
// | @date  : 2015-04-11 15:43:54
// +----------------------------------------------------------------------
// | @Last Modified by:  琥珀
// +----------------------------------------------------------------------
// | @Last Modified time: 2015-04-13 17:14:31
// +-----------------------------------------------------------------------

namespace Admin\Controller;
use Think\Controller;
use Think;
use Think\Model\RelationModel;
use Admin\Model\AuthRuleModel;
/**
*公共控制器
*/
class CommonController extends Controller{

    /**
     * Bjui系统用到参数  ajax赋值扩展
     * @access protected
     * @param array $result  引用传值
     * @return void
     */
    protected function ajaxAssign(&$result) {
        $result['statusCode'] = $result['status'];
        $result['closeCurrent'] = $_REQUEST['closeCurrent'];
        $result['message'] = $result['info'];
        $result['tabid'] = $_REQUEST['tabid'];
        $result['dialogid'] = $_REQUEST['dialogid'];
        $result['refresh'] = $_REQUEST['refresh'];
        $result['forward'] = $_REQUEST['forward'];
        $result['forwardConfirm'] = $_REQUEST['forwardConfirm'];
    }

    /**
     * Bjui系统用到参数  ajax赋值扩展 ,继承Controller的ajaxReturn
     * @access protected
     * @param array $data,$type  引用传值
     * @return void
     */
    protected function ajaxReturn($data, $type = '', $json_option = 0) {
        if (method_exists($this, "ajaxAssign")) $this->ajaxAssign($data);
        parent::ajaxReturn($data, $type);
    }
    /**
     * 通用分页列表数据集获取方法
     * @return array|false
     * 返回数据集
     */
    protected function lists($model, $where = array(), $order = '', $field = true) {
        $options = array();
        $REQUEST = (array)I('request.');
        if (is_string($model)) {
            $model = M($model);
        }
        $OPT = new \ReflectionProperty($model, 'options');
        $OPT->setAccessible(true);
        $pk = $model->getPk();
        if ($order === null) {
        } else if (isset($REQUEST['orderDirection']) && isset($REQUEST['orderField']) && in_array(strtolower($REQUEST['orderDirection']), array('desc', 'asc'))) {
            $options['order'] = '`' . $REQUEST['orderField'] . '` ' . $REQUEST['orderDirection'];
        } elseif ($order === '' && empty($options['order']) && !empty($pk)) {
            $options['order'] = $pk . ' desc';
        } elseif ($order) {
            $options['order'] = $order;
        }
        unset($REQUEST['orderDirection'], $REQUEST['orderField']);
        if (empty($where)) {
        }
        if (!empty($where)) {
            $options['where'] = $where;
        }
        $options = array_merge((array)$OPT->getValue($model), $options);
        $total = $model->where($options['where'])->count();
        if (isset($REQUEST['pageSize'])) {
            $listRows = (int)$REQUEST['pageSize'];
        } else {
            $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
        }
        $firstRow = empty($REQUEST['pageCurrent']) ? '0' : $listRows * ($REQUEST['pageCurrent'] - 1);
        $options['limit'] = $firstRow . ',' . $listRows;
        $pageSize = empty($REQUEST['pageSize']) ? C('LIST_ROWS') : $REQUEST['pageSize'];
        $pageCurrent = empty($REQUEST['pageCurrent']) ? '1' : $REQUEST['pageCurrent'];
        //显示分页数
        $this->assign('total', $total);
        $this->assign('pageSize', $pageSize);
        $this->assign('pageCurrent', $pageCurrent);
        $this->assign('pagenumshown', C('PAGE_NUM_SHOWN'));
        $model->setProperty('options', $options);
        if ($model instanceof RelationModel) {
            return $model->relation(true)->field($field)->select();
        } else {
            return $model->field($field)->select();
        }
    }
}
?>